<?php

namespace app\common\service;

use app\common\service\Base;
use think\Db;

/**
 * 订单工价-对帐单管理
 */
class ProcessPriceOrder extends Base
{
    /**
     * 裁床完成时（按下 拉布确认），初始化wx_order_progress_price表，如存在就不用了。
     * @param $orderId
     * @return int|string
     */
    public function cutting_confirm_add($orderId)
    {
        //1， 查询
        $temp = db('order_progress_price')->where([
            ['order_id', '=', $orderId],
            ['process_stage', '=', 1],
        ])->count();
        if ($temp > 0) {
            return true;
        }
        //2， 初始信息
        $list = db('order o')
            ->field('o.id as order_id ,o.client_ordernum,o.sku
            ,spp.process_stage,spp.process_name,spp.process_price 
            ')
            ->where([
                ['o.id', '=', $orderId],
            ])
            ->join('sku_process_price spp', 'o.sku=spp.sku and spp.process_stage=1', 'left')
            ->select();
        foreach ($list as $k => $v) {
            $temp = db('order_progress_price')->insert([
                'order_id' => $v['order_id'],
                'client_ordernum' => $v['client_ordernum'],
                'sku' => $v['sku'],
                'process_stage' => 1,
                'process_name' => trim($v['process_name']) == '' ? '裁床' : trim($v['process_name']),
                'process_price' => floatval($v['process_price']) == 0 ? 0.3 : floatval($v['process_price']),
                'status' => 0,
                'action_str' => serialize([
                    [ADMIN_ID, time(), '裁剪完成'],
                ]),
                'create_time' => time(),
                'update_time' => time(),
            ]);
        }
        return $temp;
    }

    /**
     * （按下 拉布确认旁的 返回重置）
     * @param $orderId
     * @return int
     */
    public function cutting_resetItem_del($orderId)
    {
        return db('order_progress_price')->where([
            ['order_id', '=', $orderId],
            ['process_stage', '=', 1],
            ['status', '=', 0],
        ])->delete();
    }
}